var http = require("http"),
    cheerio = require("cheerio"),
    fs = require("fs");
var url = "http://www.toutiao.com/news_image/";
var imgPath = 'http://img.lexus.do2014.cn/images/es/car/spoke10a/Red_Mica_Crystal_Shine/';

function download(url, callback) {
    http.get(url, function (res) {
        var data = "";
        res.on('data', function (chunk) { 
            data += chunk;
        });
        res.on("end", function () {
            callback(data); 
        });
    }).on("error", function () {
        callback(null);
    });
}
download(url, function (data) {
    if (data) {
        //console.log(data);

        var $ = cheerio.load(data);
        console.log(data);
        console.log("done");
    } else {
        console.log("error");
    }
})

//console.log(_this);





// fs.mkdir('./downloadImg', (err) => {
//     if (err && err.code != 'EEXIST') return;
//     downloadImg();
// });

function downloadImg() {
    for (var i = 0; i < 60; i++) {
        var url = imgPath + i + ".jpg!t1024x450.jpg";
        // console.log(url);
        ((i) => {
            http.get(url, (res) => {
                var out = fs.createWriteStream('./downloadImg/' + i + '.jpg', {
                    encoding: 'binary'
                });

                res.on('data', (chunk) => {
                    out.write(chunk);
                });

                res.on('end', () => {
                    console.log('Image_' + i + ' download complete.');
                    out.end('');
                });
            });
        })(i);
    }
}